﻿/// Author: AustralTek 
/// Create date: 2010/05/04
/// http://www.australtek.com
/// Copyright: 2009 - 2010
/// MIT Licensed

//////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////// 

(function () {
    try {
        function _DestinationPDF(options) {
            try {
                var widgetsList;
                var exporter;
                var exporterType;
                var privateParameters = {
                    docAttributes: {
                        fileName: null
                        , marginTop: null
                        , marginLeft: null
                        , marginBottom: null
                        , marginRight: null
                    }
                    , events: {
                        onError: function () { }
                    }
                };

                function _initialize(options) {
                    try {
                        widgetsList = new Array();
                        exporter = 'DestinationPDF';
                        exporterType = 1;

                        privateParameters = new PrivateParameters(options);
                        privateParameters.DocAttributes = privateParameters.DocAttributes || new Object();
                        privateParameters.DocAttributes.FileName = ((privateParameters.DocAttributes.FileName) && (privateParameters.DocAttributes.FileName != ''))
                                                                    ? ((privateParameters.DocAttributes.FileName.match('.*(\.pdf)'))
                                                                        ? privateParameters.DocAttributes.FileName
                                                                        : privateParameters.DocAttributes.FileName + '.pdf'
                                                                        )
                                                                    : null;
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                /*-----------------------------------------------------------------------*/
                /*------------------------------Instance it------------------------------*/
                /*-----------------------------------------------------------------------*/
                _initialize(options);
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/

                function _addPdfWidget(pdfWidget) {
                    try {
                        if (pdfWidget) widgetsList.push(pdfWidget.GetWidgetData());
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _sendData() {
                    try {
                        var form = document.createElement('form');
                        form.action = privateParameters.URL;
                        form.method = 'post';
                        form.style.display = 'none';
                        document.documentElement.appendChild(form);

                        var inputDestinationPdfValue = document.createElement('input');
                        inputDestinationPdfValue.type = 'hidden';
                        inputDestinationPdfValue.name = 'DestinationPDFValue';
                        inputDestinationPdfValue.value =  JSONEncode({ Widgets:widgetsList, DocAttributes:privateParameters.DocAttributes});
                        form.appendChild(inputDestinationPdfValue);

                        var inputExporter = document.createElement('input');
                        inputExporter.type = 'hidden';
                        inputExporter.name = 'exporter';
                        inputExporter.value =  exporter;
                        form.appendChild(inputExporter);

                        var inputExporterType = document.createElement('input');
                        inputExporterType.type = 'hidden';
                        inputExporterType.name = 'exporterType';
                        inputExporterType.value =  exporterType;
                        form.appendChild(inputExporterType);
                                                
                        form.submit();
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                /*----------------*/
                /*-----Public-----*/
                /*----------------*/

                /*----------------------*/
                /*--------Methods-------*/
                /*----------------------*/
                this.AddPdfWidget = function (pdfWidget) {
                    _addPdfWidget(pdfWidget);
                }
                this.SendData = function () {
                    _sendData();
                }
            }
            catch (Error) { window.status = Error.Message; }
        }

        window.DestinationPDF = function (instanceOption) { return new _DestinationPDF(instanceOption); };
    }
    catch (Error) { window.status = Error.Message }
})();

(function () {
    try {
        function _PdfWidget(options) {
            try {
                var content;
                var privateParameters = {
//                    NameSpace: null // for Reflection
//                    , ClassName: null // for Reflection
////                    , dllFullPath: null // for Reflection
//                    , Attributes: {
//                        Width: null
//                        , Height: null
//                    }
//                    , Events: {
//                        onError: function () { }
//                    }
                };

                function _initialize(options) {
                    try {
                        content = null;

                        privateParameters = new PrivateParameters(options);
                        privateParameters.NameSpace = privateParameters.NameSpace || 'IndustrialDashboard';
                        privateParameters.ClassName = privateParameters.ClassName || null;
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                /*-----------------------------------------------------------------------*/
                /*------------------------------Instance it------------------------------*/
                /*-----------------------------------------------------------------------*/
                _initialize(options);
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/

                function _setContent(newContent) {
                    try {
                        if (!newContent) throw new Error({ message: "The \"Content\" can not be null." });
                        content = newContent;
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _getWidgetData() {
                    var data = new Object();
                    try {
                        data.Namespace = privateParameters.NameSpace;
                        data.ClassName = privateParameters.ClassName;
                        data.WidgetData = JSONEncode(content);
                        return data;
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                    return null;
                }

                /*----------------*/
                /*-----Public-----*/
                /*----------------*/

                /*----------------------*/
                /*--------Methods-------*/
                /*----------------------*/
                this.SetContent = function (newContent) {
                    _setContent(newContent);
                };
                this.GetWidgetData = function () {
                    return _getWidgetData();
                };
                this.GetNameSpace = function () {
                    return privateParameters.NameSpace;
                };
                this.SetNameSpace = function (newNameSpace) {
                    privateParameters.NameSpace = newNameSpace;
                };
                this.GetClassName = function () {
                    return privateParameters.ClassName;
                };
                this.SetClassName = function (newClassName) {
                    privateParameters.ClassName = newClassName;
                };
            }
            catch (Error) { window.status = Error.Message; }
        }

        window.PdfWidget = function (instanceOptions) { return new _PdfWidget(instanceOptions); };
    }
    catch (Error) { window.status = Error.Message; }
})();